$(function () {
    $("#workflowGrid").Grid({
        url: '../workflow/info/list',
        colModel: [
			{label: 'id', name: 'id', index: 'id', key: true, hidden: true},
			{label: '所属公司', name: 'companyName', index: 'company_id', width: 80},
			{label: '所属门店', name: 'branchName', index: 'branch_id', width: 80},
			{label: '所属部门', name: 'departName', index: 'depart_id', width: 80},
			{label: '流程编码', name: 'code', index: 'code', width: 80},
			{label: '流程名称', name: 'name', index: 'name', width: 80},
			{label: '状态', name: 'status', index: 'status', width: 80,
				formatter: function (value) {
					var result="";
					switch (value) {
						case 0:result='<span class="label label-danger">冻结</span>';break;
						case 1:result='<span class="label label-success">正常</span>';break;
						default:result='<span class="label label-success">正常</span>';break;
					}
					return result;
				}
			},
			{label: '创建时间', name: 'createTime', index: 'create_time', width: 80}]
    });
});

var changeTreeSetting={
	data: {
	    simpleData: {
	        enable: true,
	        idKey: "id",
	        pIdKey: "parentId",
	        rootPId: -1
	    },
	    key: {
	    	name: "name",
	        url: "nourl"
	    }
	},
	view: {
		expandSpeed: "fast"
	}	
};

var vm = new Vue({
	el: '#workflow',
	data: {
        showList: true,
        title: null,
		workflowInfo: {},
        BusinessTypeList:[],
        treeDataList:[],
        levelData:"",
        branchAll:[],
		ruleValidate: {
			name: [
				{required: true, message: '名称不能为空', trigger: 'blur'}
			],
			code: [
		       {required: true, message: '编码不能为空', trigger: 'blur'}
		    ]
		},
		q: {
		    name: '',
            branchId:''
		}
	},
    mounted:function(){
		this.getBusinessTypeList();
		this.getBranchAllList();
		this.getArchitectureTreeList();
	},
	methods: {
		selectDepartList:function(){
			$.fn.zTree.init($("#architectureChangeTree"), changeTreeSetting, vm.treeDataList).expandAll(true);
			openWindow({
                title: "选择上级",
                area: ['300px', '450px'],
                content: jQuery("#orgCompanyArchitectureLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var node =  $.fn.zTree.getZTreeObj("architectureChangeTree").getSelectedNodes();
                    vm.workflowInfo.departName=node[0].name;
                    vm.workflowInfo.departId=node[0].id;
                    layer.close(index);
                }
            });
		},
		getArchitectureTreeList:function(val){
			var url="../org/architecture/queryOwnAll";
			Ajax.request({
                url: url,
                async: true,
                successCallback: function (r) {
                	vm.treeDataList=r.list;
                	vm.levelData=r.levelData;
                }
            });
		},
        getBranchAllList:function(){
            Ajax.request({
                url: '../org/branch/queryAll',
                async: true,
                successCallback: function (r) {
                    vm.branchAll = r.list;
                }
            });
        },
		delCompanyEmployeeOne:function(){
			var num=vm.workflowInfo.auditEmployeeList.length;
			vm.workflowInfo.auditEmployeeList.splice(num-1,1);
		},
		selectCompanyEmployeeOne:function(){
			openWindow({
            	type: 2,
                title: "选择人员",
                area: ['70%', '90%'],
                content: ['../org/employeeInfoSelect.html'],
                btn: ['确定', '取消'],
                btn1: function (index,layero) {
                	var selectRowData = window[layero.find('iframe')[0]['name']].getSelectedRowData('#employeeInfoGrid');
                	if(selectRowData.length){
                		iview.Message.error("请选择一条数据！");
                		return;
                	}
                	var num=vm.workflowInfo.auditEmployeeList.length;
                	var employeeInfo={name:selectRowData.name,id:selectRowData.id,ordernum:num};
                	vm.workflowInfo.auditEmployeeList.push(employeeInfo);
                    layer.close(index);
                }
            });
		},
		handlecopyEmployee:function(event,name){
			var employeeId=name;
			var employeeSelectedList=vm.workflowInfo.copyEmployeeList;
			if(employeeSelectedList&&employeeSelectedList.length>0){
				for (var i = 0; i < employeeSelectedList.length; i++) {
    				if(employeeId==employeeSelectedList[i].id){
    					employeeSelectedList.splice(i,1);break;
    				}
				}
				vm.workflowInfo.copyEmployeeList=employeeSelectedList;
			}
		},
		selectCompanyEmployee:function(){
			openWindow({
            	type: 2,
                title: "选择人员",
                area: ['60%', '80%'],
                content: ['../org/employeeInfoSelect.html'],
                btn: ['确定', '取消'],
                btn1: function (index,layero) {
                	var selectRowList = window[layero.find('iframe')[0]['name']].getSelectedRowsData('#employeeInfoGrid');
                	if(selectRowList&&selectRowList.length>0){
                		for (var i = 0; i < selectRowList.length; i++) {
                			var addSelected=true;
                			for (var j = 0; j < vm.workflowInfo.copyEmployeeList.length; j++) {
                				if(selectRowList[i].id==vm.workflowInfo.copyEmployeeList[j].id){
                					addSelected=false;
                				}
							}
                			if(addSelected){
                				vm.workflowInfo.copyEmployeeList.push(selectRowList[i]);
                			}
                		}
                	}
                    layer.close(index);
                }
            });
		},
		query: function () {
			vm.reload();
		},
		add: function () {
			vm.showList = false;
			vm.title = "新增";
			vm.workflowInfo = {copyEmployeeList:[],auditEmployeeList:[],departName:'',departId:'',levelData:vm.levelData};
		},
		update: function (event) {
            var id = getSelectedRow("#workflowGrid");
			if (id == null) {
				return;
			}
            vm.getInfo(id);
            var st = vm.workflowInfo.status
			if (st == 0) {
                iview.Message.error("当前状态不能修改");
            	return;
			}
            vm.showList = false;
            vm.title = "修改";
		},
		saveOrUpdate: function (event) {
            var url = vm.workflowInfo.id == null ? "../workflow/info/save" : "../workflow/info/update";
            Ajax.request({
			    url: url,
                params: JSON.stringify(vm.workflowInfo),
                type: "POST",
			    contentType: "application/json",
                successCallback: function (r) {
			    	if (r.code == 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg, function (index) {
                            vm.reload();
                        });
					}
                }
			});
		},
		del: function (event) {
            var ids = getSelectedRows("#workflowGrid");
			if (ids == null){
				return;
			}

			confirm('确定要删除选中的记录？', function () {
                Ajax.request({
				    url: "../workflow/info/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
				    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
					}
				});
			});
		},
		getInfo: function(id){
            Ajax.request({
                url: "../workflow/info/info/"+id,
                async: false,
                successCallback: function (r) {
                    vm.workflowInfo = r.workflowInfo;
                }
            });
		},
		getBusinessTypeList:function(){
            Ajax.request({
                url: "../workflow/info/getBusinessType",
                async: true,
                successCallback: function (r) {
                    vm.BusinessTypeList = r.list;
                }
            });
		},
		reload: function (event) {
			vm.showList = true;
            var page = $("#workflowGrid").jqGrid('getGridParam', 'page');
			$("#workflowGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name,'branchId':vm.q.branchId},
                page: 1
            }).trigger("reloadGrid");
//            vm.handleReset('formValidate');
		},
        reloadSearch: function() {
            vm.q = {
                name: '',
                branchId:''
            };
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        }
	}
});